#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# ARD.PY                       -- by Dr. ZhuoQing 2021-05-24
#
# Note:
#============================================================

from head import *

arduino_title = "Arduino 1.8.15"

#------------------------------------------------------------

if len(sys.argv) == 1:
    tspeditfileclear()
    tspsendwindowkey(arduino_title, "acv", control=1)
    tsppasteclipboard()
    printf('\a')
    exit()

#------------------------------------------------------------
if sys.argv[1] == 'i':
    tspeditfileclear()

    tspinsii("//------------------------------------------------------------------------------")
    tspinsii("void setup(void) {")
    tspinsii("    ")
    tspinsii("}\r\n\r\n")
    tspinsii("//------------------------------------------------------------------------------")
    tspinsii("void loop(void) {")
    tspinsii("    ")
    tspinsii("}\r\n")

    tspeditfileinserthead()
    exit()

if sys.argv[1] == 'I':
    tspeditfileclear()

    tspinsii("#define ON(pin)                 digitalWrite(pin, HIGH)")
    tspinsii("#define OFF(pin)                digitalWrite(pin, LOW)")
    tspinsii("#define VAL(pin)                digitalRead(pin)")
    tspinsii("#define IN(pin)                 pinMode(pin, INPUT)")
    tspinsii("#define OUT(pin)                pinMode(pin, OUTPUT)")
    tspinsii("const int LED_PIN = 13;")

    tspinsii("//------------------------------------------------------------------------------")
    tspinsii("void setup(void) {")
    tspinsii("    pinMode(LED_PIN, OUTPUT);")
    tspinsii("    ")
    tspinsii("}\r\n\r\n")
    tspinsii("//------------------------------------------------------------------------------")
    tspinsii("void loop(void) {")
    tspinsii("    ")
    tspinsii("}\r\n")

    tspeditfileinserthead()
    exit()


#------------------------------------------------------------
if sys.argv[1] == 'a' or sys.argv[1] == 'ino':

    tspinsii("#define ON(pin)                 digitalWrite(pin, HIGH)")
    tspinsii("#define OFF(pin)                digitalWrite(pin, LOW)")
    tspinsii("#define VAL(pin)                digitalRead(pin)")
    tspinsii("#define IN(pin)                 pinMode(pin, INPUT)")
    tspinsii("#define INP(pin)                pinMode(pin, INPUT_PULLUP)")
    tspinsii("#define OUT(pin)                pinMode(pin, OUTPUT)")

    tspfocuswindow("TEASOFT:1")
    exit()


#------------------------------------------------------------
if sys.argv[1].find('sendhex') >= 0:

    tspinsii("unsigned char Hex2Text(unsigned char ucCode) {")
    tspinsii("    if(ucCode < 10) {")
    tspinsii("        return '0' + ucCode;")
    tspinsii("    }")
    tspinsii("    ")
    tspinsii("    return 'A' + ucCode - 10;\r\n")
    tspinsii("}\r\n")
    tspinsii("void SendHEX8(unsigned char ucCode) {")
    tspinsii("    Serial.write(Hex2Text(ucCode >> 4));")
    tspinsii("    Serial.write(Hex2Text(ucCode & 0xf));")
    tspinsii("}")

    exit()



#------------------------------------------------------------
if sys.argv[1].find('servo') >= 0:
    tspinsii("const int PWM_PIN       = 11;")
    tspinsii("const int SERVO_MID     = 47;")
    tspinsii("const int SERVO_LEFT    = 31;")
    tspinsii("const int SERVO_RIGHT   = 63;\r\n")
    tspinsii("void servoSetup(void) {")
    tspinsii("    TCCR2B = TCCR2B & B11111000 | B00000110;")
    tspinsii("    pinMode(PWM_PIN, OUTPUT);")
    tspinsii("    analogWrite(PWM_PIN, SERVO_MID);")
    tspinsii("}\r\n")

    exit()


#------------------------------------------------------------
if sys.argv[1] == '>':
    tspcopyclipboard()
    tspsendwindowkey(arduino_title, "avu", control=1)
    printf('\a')
    exit()

#------------------------------------------------------------
if sys.argv[1] == '<':
    tspsendwindowkey(arduino_title, "acv", control=1)
    tsppasteclipboard()
    printf('\a')
    exit()


#------------------------------------------------------------
tspsendwindowkey(arduino_title, "u", control=1)
printf('\a')



#------------------------------------------------------------
#        END OF FILE : ARD.PY
#============================================================
